package cn.itcast.smsreader;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Telephony;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView mTvSMS;
    private boolean grantSms=false;
    private ContentResolver resolver;
    private MyContentObserver observer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvSMS = findViewById(R.id.tv_message);

        handlePermission();
        resolver = getContentResolver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            Uri uri = Telephony.Sms.CONTENT_URI;
            observer = new MyContentObserver(new Handler());
            resolver.registerContentObserver(uri,true, observer);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(observer!=null) {
            resolver.unregisterContentObserver(observer);
        }
    }

    private void handlePermission() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int permission = checkSelfPermission(Manifest.permission.READ_SMS);
            if(permission == PackageManager.PERMISSION_GRANTED){
                grantSms = true;
                return;
            }else{
                requestPermissions(new String[]{Manifest.permission.READ_SMS},101);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 101 )
        {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                grantSms = true;
                Toast.makeText(this, "读取短信授权成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void readSMS(View view) {
        if(grantSms==false){
            Toast.makeText(this, "没有授权", Toast.LENGTH_SHORT).show();
            handlePermission();
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Cursor cursor = resolver.query(Telephony.Sms.CONTENT_URI,
                    new String[]{Telephony.Sms._ID, Telephony.Sms.ADDRESS, Telephony.Sms.BODY, Telephony.Sms.DATE_SENT},
                    null, null, null);
            StringBuffer sb = new StringBuffer();
            while (cursor.moveToNext()){

                sb.append("一条消息");
                sb.append(cursor.getString(0)+"--");
                sb.append(cursor.getString(1)+"--");
                sb.append(cursor.getString(2)+"--");
                sb.append(cursor.getString(3)+"--\n");
                mTvSMS.setText(sb.toString());
            }

        }

    }
}